<template>
  <div>
    <!--登陆头部-->
    <van-nav-bar title="面经登陆" />
    <!--表单内容-->
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label-width="60"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        label-width="60"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          style="background-color: #fa6d1d"
          round
          block
          type="info"
          native-type="submit"
          >提交</van-button
        >
      </div>
      <div style="text-align: right; padding-right: 20px">
        <router-link style="color: #0970a5" to="/register"
          >注册账号</router-link
        >
      </div>
    </van-form>
  </div>
</template>
<script>


// import request from '../utils/index'//导入axios公共函数
import { Notify } from 'vant';
import { setToken } from '../utils/storage.js'//导入缓存token公共函数
import { login } from '../api/userInfo.js'//导入api登陆接口
export default {
  data () {
    return {
      username: '987654',
      password: '654321',
    };
  },
  methods: {



    async onSubmit (values) {
      console.log('submit', values);
      try {
        const res = await login(values)
        setToken(res.data.token)//本地缓存token值
        Notify({ type: 'success', message: '登陆成功', duration: 1000, });//登陆成功提示
        setTimeout(() => {
          this.$router.push('/')
        }, 1100)
      } catch (err) {
        Notify({ type: 'danger', message: '用户名或密码不正确' })

      }



     

    },
  },
};
</script>
<style lang='less'  scoped>
</style>
